package com.slz3.business.module.messageManagement.mesType.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.messageManagement.mesType.domain.MesType;
import com.slz3.business.module.messageManagement.mesType.domain.MesTypeFactory;
import com.slz3.business.module.messageManagement.mesType.steward.MesTypeSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class MesTypeServiceImpl implements MesTypeService {
    @Override
    public MesType save(MesType mesType) {
        return Repositorys.mesType.save(MesTypeFactory.initBaseFields(mesType));
    }

    @Override
    public Page<MesType> page(MultiValueMap<String, String> params) {
        Page<MesType> mesTypePage = Repositorys.mesType.findAll(
                MesTypeSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return mesTypePage;
    }


    @Override
    public MesType modify(MesType mesTypeNew) {
        return Repositorys.mesType.saveAndFlush(MesTypeFactory.initBaseFields(mesTypeNew));
    }

    @Override
    public Optional<MesType> single(UUID uuid) {
        return Repositorys.mesType.findById(uuid);
    }

    @Override
    public List<MesType> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.mesType.findAllById(uuids);
    }

    @Override
    public List<MesType> delete(List<MesType> mesTypeList) {
        mesTypeList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MesTypeFactory.initBaseFields(item);
        });
        return Services.mesTypeTransactional.batchModify(mesTypeList);
    }

}
